<template>
  <div class="product-list-page">
    <Navbar />
    <div class="product-list-container">
      <!-- 使用新的搜索框组件 -->
      <div class="search-section">
        <h2>商品搜索</h2>
        <SearchBox @search="handleSearch" ref="searchBoxRef" />
      </div>

      <!-- 热点商品展示区域 -->
      <div v-if="hotProducts.length > 0 && !searchResults" class="hot-products-section">
        <h2>🔥 热门商品</h2>
        <div class="product-grid">
          <ProductCard
              v-for="product in hotProducts"
              :key="product.id"
              :product="product"
          />
        </div>
      </div>
      
      <!-- 搜索结果区域 -->
      <div v-if="searchResults">
        <!-- 返回搜索框按钮 -->
        <div class="return-to-search">
          <button @click="returnToHome" class="return-button">
            ← 返回首页
          </button>
        </div>
        
        <h2>搜索结果</h2>
        <div class="product-grid">
          <ProductCard
              v-for="product in searchResults"
              :key="product.id"
              :product="product"
          />
        </div>
        <div v-if="searchResults.length === 0" class="no-products">
          未找到相关商品
        </div>
      </div>
      
      <!-- 默认商品列表 -->
      <div v-if="!searchResults">
        <h1>商品列表</h1>
        <div class="product-grid">
          <ProductCard
              v-for="product in products"
              :key="product.id"
              :product="product"
          />
        </div>
        <div v-if="products.length === 0" class="no-products">
          暂无商品数据
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import Navbar from '../components/Navbar.vue';
import SearchBox from '../components/SearchBox.vue';
import ProductCard from '../components/ProductCard.vue';
import { productAPI } from '../services/api';
import { searchAPI } from '../services/api';

const products = ref([]);
const hotProducts = ref([]);
const searchResults = ref(null);
const searchBoxRef = ref(null);
const router = useRouter();
const currentSearchKeyword = ref('');

// 处理搜索事件
const handleSearch = async (keyword) => {
  if (!keyword.trim()) {
    searchResults.value = null;
    return;
  }
  
  // 保存当前搜索关键词
  currentSearchKeyword.value = keyword;
  
  try {
    const response = await searchAPI.prefixSearchDetail(keyword);
    searchResults.value = response.data || [];
  } catch (error) {
    console.error('搜索失败:', error);
    alert('搜索失败: ' + (error.message || '未知错误'));
  }
};

// 返回首页
const returnToHome = () => {
  // 清空搜索结果
  searchResults.value = null;
  
  // 重置搜索框关键词
  if (searchBoxRef.value) {
    searchBoxRef.value.setKeyword('');
  }
  
  // 导航到首页
  router.push('/');
  
  // 滚动到页面顶部
  window.scrollTo({ top: 0, behavior: 'smooth' });
};

onMounted(async () => {
  try {
    // 获取热点商品列表
    const hotResponse = await productAPI.getHotList();
    hotProducts.value = hotResponse.data || [];
    
    // 获取普通商品列表
    const response = await productAPI.getList();
    products.value = response.data;
  } catch (error) {
    console.error('获取商品列表失败:', error);
    alert('获取商品列表失败: ' + (error.message || '未知错误'));
  }
});
</script>

<style scoped>
.product-list-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

h1, h2 {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
}

h2 {
  color: #e53935;
  font-size: 1.5rem;
}

.search-section {
  margin-bottom: 30px;
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 8px;
}

.return-to-search {
  text-align: left;
  margin-bottom: 20px;
}

.return-button {
  padding: 8px 16px;
  background-color: #f5f5f5;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  color: #333;
}

.return-button:hover {
  background-color: #e0e0e0;
}

.hot-products-section {
  margin-bottom: 40px;
  padding: 20px;
  background-color: #fffaf0;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.product-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 20px;
}

.no-products {
  text-align: center;
  padding: 50px;
  color: #666;
  font-size: 1.2rem;
}
</style>